#ifndef __BUFFER_H__
#define __BUFFER_H__

#include <iostream>
#include <vector>
#include <atomic>

//选自 https://blog.csdn.net/weixin_51322383/article/details/130472317

/*
    +------------------------------------------------------+
    |  prependable  |    readable    |      writable       |
    +------------------------------------------------------+
                    ↑                ↑
                ReadIndex        WriteIndex
*/

namespace NetWork {
    class Buffer {
    private:
        std::vector<char> Buffers;

        std::atomic<std::size_t> ReadIndex;
        std::atomic<std::size_t> WriteIndex;

    public:
        Buffer(size_t bufSize = 1024);

        Buffer(const Buffer&) = delete;
        Buffer& operator=(const Buffer&) = delete;

        Buffer(Buffer&&);
        Buffer& operator=(Buffer&&);

    private:
        void MakePlace(size_t len);

    public:
        size_t GetPrependable() const;  //可预留空间
        size_t GetReadable() const;     //可读空间
        size_t GetWritable() const;     //可写空间

        void Retrieve(size_t len);      //按一定长度复原可读buffer
        void RetrieveAll();             //复原buffer

        std::vector<char> Read();
        std::vector<char> Read(size_t len);
        void Wirte(const void* buf, size_t len);

        const char* ReadableBegin() const;
        const char* ReadableEnd() const;
    };

}

#endif